package net.sf.fc.io.event;

import java.io.File;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import net.sf.fc.io.event.FileCopyEvent.CopyStatus;
import net.sf.fc.io.event.FileCopyEvent.Level;

public final class FileCopyEventSupport {

    private final List<FileCopyEventListener> listeners = new CopyOnWriteArrayList<FileCopyEventListener>();
    private final Object source;

    public FileCopyEventSupport(Object source) {
        this.source = source;
    }

    public void addFileCopyEventListener(FileCopyEventListener listener) {
        listeners.add(listener);
    }

    public void removeFileCopyEventListener(FileCopyEventListener listener) {
        if(listener != null) {
            listeners.remove(listener);
        }
    }

    public void fireStatusChanged(File srcFile, File dstFile, CopyStatus status, Level level, long bytesCopied) {
        fireStatusChanged(new FileCopyEvent(source, srcFile, dstFile, status, level, bytesCopied));
    }

    public void fireStatusChanged(File srcFile, File dstFile, CopyStatus status, Level level, long bytesCopied, Throwable exception) {
        fireStatusChanged(new FileCopyEvent(source, srcFile, dstFile, status, level, bytesCopied, exception));
    }

    public void fireStatusChanged(FileCopyEvent event) {

        for(FileCopyEventListener listener: listeners) {
            listener.statusChanged(event);
        }
    }

}
